package com.citywy.view;
 
import com.citywy.domain.News;
import com.citywy.domain.User;
import com.citywy.service.NewsService;
import com.citywy.util.PageUtil;
import com.citywy.util.PublicUtil;
import com.citywy.util.UserUtil;
import com.sun.jmx.remote.util.OrderClassLoaders;
import com.sun.org.apache.xpath.internal.operations.Or;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

 
/**
 * Servlet implementation class KnowServlet
 */
public class NewsServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public NewsServlet() {
        super();
    }
 
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
 
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		String uri=request.getRequestURI();
		String type=uri.substring(uri.lastIndexOf("/")+1,uri.lastIndexOf("."));
		System.out.println("输出ajax的path:"+type+"----uri:"+uri);
		//跳转到对应的增删改查
		if(type!=null){
			if("load".equals(type)){
				load(request,response);
			}else if("modify".equals(type)){
				modify(request,response);
			}else if("list".equals(type)){
				list(request,response);
			}else if("del".equals(type)){
				del(request,response);
			}
		}

	}

	//加载页面，包括新页面和修改页面
	public void load(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		Map<Integer,String> menus = new HashMap<Integer,String>();
		request.setAttribute("menus",menus);
		request.getRequestDispatcher("/admin/news/news_load.jsp").forward(request, response);
	}

	/**
	 * 增加和修改
	 * @param request
	 * @param response
	 */
	public void modify(HttpServletRequest request,HttpServletResponse response) throws IOException {
		NewsService newsService = new NewsService();
		User user = UserUtil.getLoginUser(request);
		String title=PublicUtil.getParam(request,"title",null);
		String source=PublicUtil.getParam(request,"source",null);
		String logo = PublicUtil.getParam(request,"logo",null);
		int eMenu = PublicUtil.getParam(request,"Emenu",0);
		String tag =PublicUtil.getParam(request,"tag",null);
		String summary = PublicUtil.getParam(request,"summary",null);
		String content = PublicUtil.getParam(request,"content",null);
		News news = new News();
		news.setTitle(title);
		news.setSource(source);
		news.setLogo(logo);
		news.setEmenu(eMenu);
		news.setTag(tag);
		news.setSummary(summary);
		news.setContent(content);
		news.setEaid(user.getId());
		boolean b = newsService.addNews(news);
		System.out.println("插入新闻："+b);
		response.sendRedirect("/servlets/news/list.htm");
	}

	/**
	 * 查询列表
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void list(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
		NewsService newsService=new NewsService();
		//获取参数
		int pageNow=PublicUtil.getParam(request,"x_now",1);
		int pageSize=PublicUtil.getParam(request,"x_size",20);
		String x_order=PublicUtil.getParam(request,"x_order","id");//根据什么字段排序
		String x_sort=PublicUtil.getParam(request,"x_sort","desc");
		String x_name=PublicUtil.getParam(request,"x_name",null);

		//查询条件
		String condition = null;
		if(x_name!=null && !"".equals(x_name)){
			condition = "title like '%"+x_name+"%'";
		}

		int rowcount=newsService.getRowCount(condition);
		int pageCount=(rowcount-1)/pageSize+1;

		List<News> list=newsService.getNewsList(condition,pageNow,pageSize,x_order,x_sort);

		//分页参数
		PageUtil pageUtil = new PageUtil(pageNow,pageSize,pageCount,rowcount,x_order,x_sort,x_name);

		request.setAttribute("list", list);
		request.setAttribute("pageutil",pageUtil);
		request.getRequestDispatcher("/admin/news/news_list.jsp").forward(request, response);

	}

	public void del(HttpServletRequest request,HttpServletResponse response){

	}

}